package com.macro.ai.chat.controller;

import com.alibaba.fastjson.JSON;
import com.macro.ai.chat.ai.Answer;
import com.macro.ai.chat.ai.Choices;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;

import java.util.List;

@RestController
//@CrossOrigin(origins = "http://localhost:8081")
public class ChatAiController {

    @Autowired
    private ChatClient chatClient;

    //角色预设，使用非流式响应
    @GetMapping("/chatai")
    public String chatAi(@RequestParam(value = "message") String message){
        return chatClient.prompt().user(message).call().content();
    }

    //角色预设，使用流式响应，逐个字符输出，注意设置格式和编码方式，不然会乱码
    @GetMapping(value = "/chataiStream", produces = "text/event-stream;charset=UTF-8")
//    @GetMapping(value = "/chataiStream", produces = "text/html;charset=UTF-8")
    public Flux<String> chatAiStream(@RequestParam(value = "message") String message){
        System.out.println("start");
        return chatClient.prompt().user(message).stream().content();
    }
}
